(function($) {
    var arkflow = $.arkflow;

    arkflow.PathPoint = Base.extend({
        constructor : function(type, centerNode, leftNode, rightNode, path) {
                this.pointType = type;
                this.leftNode = leftNode;
                this.rightNode = rightNode;
                this.centerNode = centerNode;
                this.paper = path.paper;
                this.pathConfig = path.pathConfig;
                this.path = path;

                this.X = 0;
                this.Y = 0;

                this.bindStrategy(this.pointType);

                this.rect = this.strategy.createPoint();

                if (this.rect && this.strategy.dragable()) {
                    this.bindDrag();
                }
        },
        bindStrategy: function(pointType) {
            switch (pointType) {
                case "from" :
                    this.strategy = new arkflow.PathFromPointStrategy(this);
                    break;
                case "big" :
                    this.strategy = new arkflow.PathBigPointStrategy(this);
                    break;
                case "small" :
                    this.strategy = new arkflow.PathSmallPointStrategy(this);
                    break;
                case "to" :
                    this.strategy = new arkflow.PathToPointStrategy(this);
                    break;
            }
        },
        bindDrag: function() {
            this.rect.drag(function(_x, _y) {
                movo(_x, _y);
            }, function() {
                setPosition();
            }, function() {
            });
            var me = this;
            var movo = function(_x, _y) {
                var _X = (me.X + _x);
                var _Y = (me.Y + _y);
                me.moveTo(_X, _Y);
            };

            var setPosition = function() {
                if (me.pointType == "big") {
                    me.X = me.rect.attr("x") + me.pathConfig.attr.bigDot.width / 2;
                    me.Y = me.rect.attr("y") + me.pathConfig.attr.bigDot.height / 2;
                }
                if (me.pointType == "small") {
                    me.X = me.rect.attr("x") + me.pathConfig.attr.smallDot.width / 2;
                    me.Y = me.rect.attr("y") + me.pathConfig.attr.smallDot.height / 2;
                }
            };
        },
        type : function(v) {
            if (v) {
                this.pointType = v;
            } else {
                return this.pointType;
            }
        },node: function(v) {
            if (v) {
                this.rect = v;
            } else {
                return this.rect;
            }
        },left :function(v) {
            if (v) {
                this.leftNode = v;
            } else {
                return this.leftNode;
            }
        },right : function(v) {
            if (v) {
                this.rightNode = v;
            } else {
                return this.rightNode;
            }
        },remove: function() {
            this.leftNode = null;
            this.rightNode = null;
            this.rect.remove();
        },pos : function(P) {
            if (P) {
                this.centerNode = P;
                this.rect.attr({
                    x : this.centerNode.x - this.rect.attr("width") / 2,
                    y : this.centerNode.y - this.rect.attr("height") / 2
                });
                return this;
            } else {
                return this.centerNode;
            }
        },
        moveTo : function(x, y) {
            this.pos({
                x : x,
                y : y
            });

            this.strategy.doMoveTo(x, y);

            this.path.drawPath();
        }
    });
})(jQuery);
